<template>
	<view>
		<view class="uni-list">
			<block v-for="(value, index) in listData" :key="index">
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" @click="goDetail(value)">
					<view class="uni-media-list">
						<image class="uni-media-list-logo" :src= "value.titleImg ? value.titleImg : moren" mode="widthFix"></image>
						<view class="uni-media-list-body">
							<view class="uni-media-list-text-top">{{ value.title }}</view>
							<view class="uni-media-list-text-bottom">
								<text>{{ value.author }}</text>
								<text>{{ formatDateTime(value.sortDate) }}</text>
							</view>
						</view>
					</view>
				</view>
			</block>
		</view>
	</view>
</template>

<script>
	import request from '../../util/request.js';
	import api from '../../util/api.js';
	
	import {
		dateUtils
	} from '../../util/util.js';
	export default {
		data() {
			return {
				maxPage:0,
				pageNo:1,
				pageSize:10,
				status: 'more',
				format:'json',
				username:'linker',
				banner: {},
				listData:[],
				Boolean:true,
				moren:'/static/logo.png',
				siteId:'c859d5588b744fe28f83064b037d3890',
				NumberPages:'',
				tem:''
			};
		},
		onShow() {

		},
		onLoad() {
			this.jiazaizhong();
			this.getList();
		},
		onPullDownRefresh() {
			this.getList();
		},
		onReachBottom() {
			if (this.pageNo > this.NumberPages) {
				uni.showToast({
					title: '已经到底啦',
					icon: 'none'
				});
				return false;
		}
			this.jiazaizhong();
			this.pageNo = this.pageNo + 1;
			this.getConcatList();
		},
		methods: {
			jiazaizhong(){
				if(this.Boolean){
					uni.showLoading({
					    title: '加载中'
					});
				}
			},
			getList() {
				let url = api.findbysiteid;
					url = url + "?siteId=" + this.siteId + "&format=" + "&pageNo=" + this.pageNo + "&pageSize=" + this.pageSize + "&session=&username=" + this.username;
				request.getf(url, {
					username:this.username
				}).then(response => {
					if(response.data.code === 0){
						this.listData = response.data.data.list;
						this.maxPage = response.data.data.count;
						this.tem = this.maxPage / 10;
						this.NumberPages = this.tem > 1 ? this.tem : 1;
						uni.stopPullDownRefresh();
						uni.hideLoading();
					} else {
						if (ret.data.data.list.length < 1) {
							uni.showToast({
								title: '没有数据了',
								icon: 'none'
							});
							return false;
						}
						uni.showToast({
							icon:'none',
							title:'请求失败，' + response.data.msg,
						})
					}
				}).catch(ex=>{
					uni.showToast({
						icon:'none',
						title:'请求失败，请稍后再试'
					})
				})
			},
			getConcatList() {
				let url = api.findbysiteid;
					url = url + "?siteId=" + this.siteId + "&format=" + "&pageNo=" + this.pageNo + "&pageSize=" + this.pageSize + "&session=&username=" + this.username;
				request.getf(url, {
					username:this.username
				}).then(response => {
					if(response.data.code === 0){
						this.listData = this.listData.concat(response.data.data.list); 
						uni.hideLoading();
					} else {
						uni.showToast({
							icon:'none',
							title:'请求失败，' + response.data.msg,
						})
					}
				}).catch(ex=>{
					console.log('ex=',ex);
					uni.showToast({
						icon:'none',
						title:'请求失败，请稍后再试'
					})
				})
			},
			complete: function() {
				this.loading = false;
			},
			goDetail: function(e) {
				let detail = {
					title: e.title,
					author: e.author,
					date: e.sortDate,
					txt:e.infoText.txt,
					id:e.id,
					channel:e.channel.id
				};
				uni.navigateTo({
					url: '/pages/goDetail/goDetail?content=' + encodeURIComponent(JSON
						.stringify(detail))
				});
			},
			setTime: function(items) {
				var newItems = [];
				items.forEach(e => {
					newItems.push({
						author_name: e.author_name,
						cover: e.cover,
						id: e.id,
						post_id: e.post_id,
						published_at: dateUtils.format(e.published_at),
						title: e.title
					});
				});
				return newItems;
			},
			aderror(e) {
				
			},
			  addZero(num) {
			    return num < 10 ? '0' + num : num;
			  },
			  formatDateTime(date) {
			    const time = new Date(Date.parse(date));
			    time.setTime(time.setHours(time.getHours() + 8));
			    const Y = time.getFullYear() + '.';
			    const M = this.addZero(time.getMonth() + 1) + '.';
			    const D = this.addZero(time.getDate()) + ' ';
			    const h = this.addZero(time.getHours()) + ':';
			    const m = this.addZero(time.getMinutes()) + ':';
			    const s = this.addZero(time.getSeconds());
			    return Y + M + D + h + m + s;
			  }
		}
	};
</script>

<style>
	.uni-media-list-logo {
		width: 180rpx;
		height: 140rpx;
	}

	.uni-media-list-body {
		height: auto;
		justify-content: space-around;
	}

	.uni-media-list-text-top {
		height: 74rpx;
		font-size: 28rpx;
		overflow: hidden;
	}

	.uni-media-list-text-bottom {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}
	
	.uni-media-list {
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		width: 750px;
		flex-direction: row;
	}
	
	.loadMore {
		margin-top:0px;
		width: 100%;
		line-height: 10em;
		font-size: 30px;
		color: #555;
	
	}
	
	
	.uni-list-cell {
		position: relative;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}
	
	.uni-list {
		background-color: #FFFFFF;
		position: absolute;
		width: 100%;
		display: flex;
		margin-bottom: 100px;
	}
	
</style>
